package com.tjtt.tdm.filemodel.dao.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.filemodel.bean.Filemode;
import com.tjtt.tdm.filemodel.bean.PrintReportMode;
import com.tjtt.tdm.filemodel.bean.TdmCentificatemodeItem;
import com.tjtt.tdm.filemodel.dao.IFilemodeDao;
import com.tjtt.tdm.form.bean.AutoFormsField;
import com.tjtt.tdm.form.bean.AutoFormsNew;

public class FilemodeDaoImpl extends BaseHibernateDao implements IFilemodeDao {

	
	public Filemode getFilemodeById(long id) {
		Filemode filemode = (Filemode) super.getHibernateTemplate().get(Filemode.class ,id);
		return filemode;
	}

	
	public PageResult queryFilemodeList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from TDM_FILE_MODE where 1=1 ");
		queryString.append("select t.id,t.filename,t.filepath,t.org_name,t.gongyi_name,t.createtime from tdm_file_mode t  where 1=1 ");
		
		if (filterMap!= null && !filterMap.isEmpty()) {
			if(null != filterMap.get("modelCatalogue") &&! "".equals(filterMap.get("modelCatalogue"))){
				queryString.append(" and MODEL_CATALOGUE = '" +  filterMap.get("modelCatalogue") + "' ");
				queryCountString.append(" and MODEL_CATALOGUE = '" +  filterMap.get("modelCatalogue") + "' ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append("order by " + sidx + " " + sord + " ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}


	
	public void save(Object o) throws DaoException {
		if(o == null){
			throw new DaoException("保存时传入参数为空，保存失败");
		}
		try{
			getHibernateTemplate().save(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new DaoException("保存失败，请确认操作是否正确",e);
		}
	}

	
	public void update(Object o) throws DaoException{
		if(o == null){
			throw new DaoException("保存时传入参数为空，保存失败");
		}
		try{
			getHibernateTemplate().update(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new DaoException("保存失败，请确认操作是否正确",e);
		}
	}

	
	public void delete(Filemode filemode) throws DaoException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(filemode);
	}
	
		/**
		 * 通过选中节点查询分类信息
		 * @param pId
		 * @param filterMap
		 * @return PageResult
		 * @author 谈泊麟
		 */
		public PageResult getModelByPid(int type, Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			queryCountString.append("select count(*) from TDM_FILE_MODE where 1=1");
			queryString.append("select ID,FILENAME,to_char(CREATETIME,'yyyy-MM-dd HH24:mi:ss'),to_char(MODIFYTIME,'yyyy-MM-dd HH24:mi:ss'),FILEPATH from TDM_FILE_MODE  where  1=1");
			if(type!=1){
				queryString.append(" and TYPE ="+type);
				queryCountString.append(" and TYPE ="+type);
			}
			if (filterMap!= null && !filterMap.isEmpty()) {
				if(null != filterMap.get("modelCatalogue") &&! "".equals(filterMap.get("modelCatalogue"))){
					queryString.append(" and MODEL_CATALOGUE = '" +  filterMap.get("modelCatalogue") + "' ");
					queryCountString.append(" and MODEL_CATALOGUE = '" +  filterMap.get("modelCatalogue") + "' ");
				}
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append("order by " + sidx + " " + sord + " ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}

		
		public PageResult queryModeItemList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from TDM_FILEMODE_ITEM t where 1=1 ");
			queryString.append("select t.id,t.item_name,t.PROJECT_NAME,t.method_name,t.standard_name,t.form_cname,t.form_ename,t.field_cname,t.field_ename,t.BQ_TYPE from TDM_FILEMODE_ITEM t   where 1=1 ");
			
			if (filterMap!= null && !filterMap.isEmpty()) {
				if(null != filterMap.get("fileModeId") &&! "".equals(filterMap.get("fileModeId"))){
					queryString.append(" and t.file_mode_id = '" +  filterMap.get("fileModeId") + "' ");
					queryCountString.append(" and t.file_mode_id = '" +  filterMap.get("fileModeId") + "' ");
				}
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append("order by " + sidx + " " + sord + " ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}
		
		public PageResult queryItemList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from (select distinct t.id,t.t_itemname,t.t_jiaceleibie from gwm_item t where 1=1   ");
			queryString.append("select distinct t.id,t.t_itemname,t.T_JIANCECANSHU,t.t_jiaceleibie,t.T_JIANCEDANWEI_NAME,t.T_JIANCEDIDIAN from gwm_item t where 1=1  ");
			
			if (filterMap!= null && !filterMap.isEmpty()) {
				if (filterMap.get("itemname")!=null&&!filterMap.get("itemname").equals("")){
					String itemname = "";
					try {
						itemname = java.net.URLDecoder.decode(filterMap.get("itemname"),"UTF-8").trim();
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					queryCountString.append(" and t_itemname like '%"+itemname+"%'  ");
					queryString.append(" and t_itemname like '%"+itemname+"%'  ");
				}
				if (filterMap.get("jianceleibie")!=null&&!filterMap.get("jianceleibie").equals("")){
					String jianceleibie = "";
					try {
						jianceleibie = java.net.URLDecoder.decode(filterMap.get("jianceleibie"),"UTF-8").trim();
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					queryCountString.append(" and t_jiaceleibie like '%"+jianceleibie+"%'  ");
					queryString.append(" and t_jiaceleibie like '%"+jianceleibie+"%'  ");
				}
				String sord = (String) filterMap.get("sord");
				queryString.append(" order by  t.t_itemname " + sord + " ");
			}
			queryCountString.append(")");
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}

		
		public AutoFormsField findAuToFormsField(Long ids) {
			AutoFormsField autoFormsField = (AutoFormsField) super.getHibernateTemplate().get(AutoFormsField.class ,ids);
			return autoFormsField;
		}

		
		public AutoFormsNew findAuToFormsNew(Long formsId) {
			AutoFormsNew autoFormsNew = (AutoFormsNew) super.getHibernateTemplate().get(AutoFormsNew.class ,formsId);
			return autoFormsNew;
		}

		
		public PageResult queryCentificateItemList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from TDM_CENTIFICATEMODE_ITEM t where 1=1 ");
			queryString.append("select t.id,t.item_name,t.method_name,t.standard_name,t.form_cname,t.form_ename,t.field_cname,t.field_ename,t.BQ_TYPE from TDM_CENTIFICATEMODE_ITEM t   where 1=1 ");
			
			if (filterMap!= null && !filterMap.isEmpty()) {
				if(null != filterMap.get("fileModeId") &&! "".equals(filterMap.get("fileModeId"))){
					queryString.append(" and t.CENTIFICATE_MODE_ID = '" +  filterMap.get("fileModeId") + "' ");
					queryCountString.append(" and t.CENTIFICATE_MODE_ID = '" +  filterMap.get("fileModeId") + "' ");
				}
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append("order by " + sidx + " " + sord + " ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}

		
		public PageResult queryPrintReportItemList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from TDM_PRINTREPORTMODE_ITEM t where 1=1 ");
			queryString.append("select t.id,t.item_name,t.method_name,t.standard_name,t.form_cname,t.form_ename,t.field_cname,t.field_ename,t.BQ_TYPE from TDM_PRINTREPORTMODE_ITEM t   where 1=1 ");
			
			if (filterMap!= null && !filterMap.isEmpty()) {
				if(null != filterMap.get("fileModeId") &&! "".equals(filterMap.get("fileModeId"))){
					queryString.append(" and t.file_mode_id = '" +  filterMap.get("fileModeId") + "' ");
					queryCountString.append(" and t.file_mode_id = '" +  filterMap.get("fileModeId") + "' ");
				}
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append("order by " + sidx + " " + sord + " ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}
		/**
		 * 
		 *方法描述:查询合格证标签
		 *@author: wangxd
		 *@date: 2013-12-12下午04:54:39
		 *@param id
		 *@return
		 */
		public TdmCentificatemodeItem queryModeItemById(Long id){
			TdmCentificatemodeItem modeItem = null;
			if(null!=id){
				modeItem = (TdmCentificatemodeItem) super.getHibernateTemplate().get(TdmCentificatemodeItem.class, id);
			}
			return modeItem;
		}

		
		public PageResult queryJiShuTiaoJianList(Map filterMap) {
			StringBuffer queryString = new StringBuffer();
			queryString.append("select t.ID,t.T_JSTJ,t.T_CLPH,t.T_SFSJ,t.T_GUIH,t.T_JLLB,t.T_ZT,t.T_JCS,t.T_XMZ,t.T_JYXM,t.T_JCFF,t.T_FSJ,t.T_XMDJ,t.T_BZYQ,t.T_PDSXFH,t.T_PDSX,t.T_PDXX,t.T_PDXXFH,t.T_SXPC,t.T_XXPC,t.T_BLWS,t.T_PDGS,t.T_PDDJ,t.T_XMSJDW,t.T_JGFS,t.T_JGSL,t.T_JGJG,t.T_RCLXM,t.T_RCLJG,t.T_BZ,t.T_XMSX,t.T_PGSSX,t.T_PDGSXX from DIC_ITEM t  where 1=1 ");
			StringBuffer countString = new StringBuffer();
			countString.append("SELECT COUNT(*) FROM DIC_ITEM t where 1=1 ");
			if (filterMap != null && !filterMap.isEmpty()) {
				String filterProperty;
				String filterValue;
				filterProperty = "ids";
				filterValue = (String) filterMap.get(filterProperty);
				if (!StringUtil.isMissing(filterValue)) {
					queryString.append(" and t.ID").append(" in (").append(filterValue).append(")");
					countString.append(" and t.ID").append(" in (").append(filterValue).append(")");
				} 
				filterProperty = "T_JYXM";
				filterValue = (String) filterMap.get(filterProperty);
				if (!StringUtil.isMissing(filterValue)) {
					queryString.append(" and t.T_JYXM").append(" like '%").append(filterValue).append("%'");
					countString.append(" and t.T_JYXM").append(" like '%").append(filterValue).append("%'");
				} 
				filterProperty = "T_JSTJ";
				filterValue = (String) filterMap.get(filterProperty);
				if (!StringUtil.isMissing(filterValue)) {
					queryString.append(" and t.T_JSTJ").append(" like '%").append(filterValue).append("%'");
					countString.append(" and t.T_JSTJ").append(" like '%").append(filterValue).append("%'");
				} 
				filterProperty = "T_CLPH";
				filterValue = (String) filterMap.get(filterProperty);
				if (!StringUtil.isMissing(filterValue)) {
					queryString.append(" and t.T_CLPH").append(" like '%").append(filterValue).append("%'");
					countString.append(" and t.T_CLPH").append(" like '%").append(filterValue).append("%'");
				} 
				filterProperty = "T_JCS";
				filterValue = (String) filterMap.get(filterProperty);
				if (!StringUtil.isMissing(filterValue)) {
					queryString.append(" and t.T_JCS").append(" like '%").append(filterValue).append("%'");
					countString.append(" and t.T_JCS").append(" like '%").append(filterValue).append("%'");
				} 
				String sidx = (String)filterMap.get("sidx");
				String sord = (String)filterMap.get("sord");
				queryString.append(" order by t." + sidx + " " + sord );
			}
			return searchBySQL(countString.toString(),queryString.toString(),filterMap);
		}
		
		public PageResult queryTempletList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from (select t.id,t.mode_name,t.button_name,t.choose_dept,t.choose_form,t.mode_path from PRINT_REPORT_MODE t where 1=1    ");
							queryString.append("select t.id,t.mode_name,t.button_name,t.choose_dept,t.choose_form,t.mode_path from PRINT_REPORT_MODE t where 1=1  ");
			if (filterMap != null && !filterMap.isEmpty()) {
				if(null != filterMap.get("modelname") &&! "".equals(filterMap.get("modelname"))){
					queryString.append("and t.mode_name like '%" +  filterMap.get("modelname") + "%' ");
					queryCountString.append("and t.mode_name like '%" +  filterMap.get("modelname") + "%' ");
				}
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append(" order by " + sidx + " " + sord + " ");
				queryCountString.append(" ) ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		
		}
		/**
		 * 修改打印模版
		 */
		public PrintReportMode toUpdateTemplet(Long id) {
			PrintReportMode printReportMode = (PrintReportMode)super.getHibernateTemplate().get(PrintReportMode.class, id);
			return printReportMode;
		}
		/**
		 * 删除打印模版
		 * @param printReportMode
		 */
		public void delete(PrintReportMode printReportMode) {
			super.getHibernateTemplate().delete(printReportMode);
		}
		/**
		 * 选择表单列表
		 * @author zhn
		 * @date Dec 2, 2013 5:45:46 PM
		 */
		public PageResult queryFormList(Map<String, String> filterMap) {
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from (select t.id,t.form_cn_name,t.form_en_name from AUTO_FORMS_NEW t ");
							queryString.append("select t.id,t.form_cn_name,t.form_en_name from AUTO_FORMS_NEW t ");
			if (filterMap != null && !filterMap.isEmpty()) {
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append(" order by " + sidx + " " + sord + " ");
				queryCountString.append(" ) ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}
		
		public PageResult queryMergecelllist(Map filterMap){
			StringBuffer queryString = new StringBuffer();
			StringBuffer queryCountString = new StringBuffer();
			
			queryCountString.append("select count(*) from (select t.id from mergecell t ");
							queryString.append("select t.id,t.tableindex,t.startrow,t.startcolumn,t.endrow,t.endcolumn,t.result from mergecell t where t.filemodeid='"+filterMap.get("filemodeid")+"'");
			if (filterMap != null && !filterMap.isEmpty()) {
				String sidx = (String) filterMap.get("sidx");
				String sord = (String) filterMap.get("sord");
				queryString.append(" order by " + sidx + " " + sord + " ");
				queryCountString.append(" ) ");
			}
			return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
		}
		
}
